package User;

import java.util.Comparator;

public class Rating implements Comparable<Rating>
{
	private int id;
	private double rating;
	private RatingCompare compare = RatingCompare.id;
	
	public enum RatingCompare
	{
		rating,
		id
	}
	
	public Rating(int id, double rating)
	{
		this.id = id;
		this.rating = rating;
	}

	public int getId()
	{
		return id;
	}

	public double getRating()
	{
		return rating;
	}

	public void setRating(double rating)
	{
		this.rating = rating;
	}

	public boolean equals(Rating r)
	{
		return getId() == r.getId();
	}
	
	public void setCompare(RatingCompare compare)
	{
		this.compare = compare;
	}
	
	public RatingCompare getCompare()
	{
		return compare;
	}

	@Override
	public int compareTo(Rating r)
	{
		if (compare.equals(RatingCompare.id))
		{
			return Integer.compare(getId(), r.getId());
		}
		
		return Double.compare(r.getRating(), getRating());
	}

	public String toString()
	{
		return getId() + ": " + getRating();
	}

	public static Comparator<Rating> RatingComparator = new Comparator<Rating>()
	{
		public int compare(Rating r1, Rating r2)
		{
			RatingCompare old = r1.getCompare();
			r1.setCompare(RatingCompare.rating);
			int result = r1.compareTo(r2);
			r1.setCompare(old);
			
			return result;
		}
	};

	public static Comparator<Rating> IdComparator = new Comparator<Rating>()
	{
		public int compare(Rating r1, Rating r2)
		{
			RatingCompare old = r1.getCompare();
			r1.setCompare(RatingCompare.id);
			int result = r1.compareTo(r2);
			r1.setCompare(old);
			
			return result;
		}
	};
}
